package com.yuju.controller.group;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.GroupReturnCodeEnum;
import com.yuju.group.enums.LogBizRecordCodeEnum;
import com.yuju.log.model.LogBizRecordModel;
import com.yuju.log.service.ILogBizRecordService;
import com.yuju.log.service.ILogBizTypeService;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.util.Utils;
import com.yuju.vo.group.LogBizRecordVO;
import com.yuju.vo.group.LogBizTypeVO;
/**
 * 
 * 类名：GroupController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:37:03  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("log")
public class LogBizRecordController extends BaseController<LogBizRecordVO, LogBizRecordModel> {
	@Autowired
	private ILogBizRecordService logBizRecordService;
	@Autowired
	private IRoomOrderService roomOrderService;
	@Autowired
	private ILogBizTypeService logBizTypeService;
	
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		LogBizRecordVO LogBizRecordVO = jsonObject.toJavaObject(LogBizRecordVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(LogBizRecordVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = logBizRecordService.pageList(convert(LogBizRecordVO), LogBizRecordVO.getPage(), LogBizRecordVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(LogBizRecordCodeEnum.LOG_QUERY_SUCCESS.getCode());
			result.setReturnMsg(LogBizRecordCodeEnum.LOG_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 异动报表
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2018年2月6日 下午2:34:19 <br/>
	 */
	@RequestMapping("listForOrderLog")
	@ResponseBody
	public Result listForOrderLog(@RequestBody JSONObject jsonObject)   {
		LogBizRecordVO LogBizRecordVO = jsonObject.toJavaObject(LogBizRecordVO.class);
		Integer orderType = jsonObject.getInteger("orderType");
		// 参数校验
		Result result = new Result();
		List<LogBizRecordModel> list= roomOrderService.listForLog(convert(LogBizRecordVO),
				orderType,null);
		if (Utils.isNotNull(list) && list.size()>0) {
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(LogBizRecordCodeEnum.LOG_QUERY_SUCCESS.getCode());
			result.setReturnMsg(LogBizRecordCodeEnum.LOG_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		LogBizRecordVO LogBizRecordVO = jsonObject.toJavaObject(LogBizRecordVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(LogBizRecordVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		
		Result result = new Result();
		int rows = logBizRecordService.del(LogBizRecordVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_DEL_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_DEL_FAILURE.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_DEL_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		String id = jsonObject.getString("groupId");
		LogBizRecordVO LogBizRecordVO = jsonObject.toJavaObject(LogBizRecordVO.class);
		LogBizRecordVO.setId(id);
		Result result = new Result();
		LogBizRecordModel LogBizRecordModel = logBizRecordService.find(convert(LogBizRecordVO));
		if (Utils.isNotNull(LogBizRecordModel)) {
			result.setData(LogBizRecordModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
}
